package lzyc.pointsManagement.business.service.impl;


import lombok.Getter;
import lzyc.pointsManagement.business.activity.convertor.ActivityInfoConvertor;
import lzyc.pointsManagement.business.activity.domain.ActivityInfo;
import lzyc.pointsManagement.business.activity.dto.ActivityInfoDto;
import lzyc.pointsManagement.business.activity.mapper.ActivityInfoMapper;
import lzyc.pointsManagement.business.attachment.convertor.AttachmentsConvertor;
import lzyc.pointsManagement.business.attachment.domain.Attachments;
import lzyc.pointsManagement.business.attachment.dto.AttachmentsDto;
import lzyc.pointsManagement.business.attachment.mapper.AttachmentsMapper;
import lzyc.pointsManagement.business.service.ActivityInfoService;
import lzyc.pointsManagement.business.service.AttachmentsService;
import lzyc.pointsManagement.common.core.page.PageDomain;
import lzyc.pointsManagement.common.response.ResponseMessage;
import lzyc.pointsManagement.common.response.ResponseMessageUtility;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.ZoneId;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
* @author liuyuan
* @description 针对表【learning_activity】的数据库操作Service实现
* @createDate 2024-03-08 22:05:33
*/


@Getter
@Service
public class ActivityInfoServiceImpl implements ActivityInfoService {

    private final ActivityInfoMapper activityInfoMapper;
    private final AttachmentsService attachmentsService;
    private final AttachmentsMapper attachmentsMapper;

    public ActivityInfoServiceImpl(ActivityInfoMapper activityInfoMapper, AttachmentsService attachmentsService, AttachmentsMapper attachmentsMapper) {
        this.attachmentsMapper = attachmentsMapper;
        this.activityInfoMapper = activityInfoMapper;
        this.attachmentsService = attachmentsService;
    }

    @Override
    public ResponseMessage addActivityInfo(ActivityInfoDto activityInfoDto) {
        ResponseMessage responseMessage;
        List<String> images = activityInfoDto.getImage();
        ActivityInfo activityInfo = ActivityInfoConvertor.convertToEntity(activityInfoDto);
        if (activityInfo.getEndTime() == null) {
            return ResponseMessageUtility.createError("结束时间不得为空。");
        }
        attachmentsService.batchUpdateBizIds(images, activityInfo.getId());
        activityInfoMapper.addActivityInfo(activityInfo);
        return ResponseMessageUtility.createSuccess();
    }

    @Override
    public ResponseMessage getAllActivityInfo(PageDomain pageDomain) {
        ResponseMessage result = ResponseMessageUtility.createSuccess();
        int offset = (pageDomain.getPageNum() - 1) * pageDomain.getPageSize();
        int size = pageDomain.getPageSize();
        List<ActivityInfo> activityInfoList = activityInfoMapper.selectActivityInfoListByPage(offset, size);
        List<ActivityInfoDto> dtoList = new ArrayList<>();
        if (activityInfoList != null) {
            for (ActivityInfo activityInfo : activityInfoList) {
                ActivityInfoDto activityInfoDto = ActivityInfoConvertor.convertToDto(activityInfo);
                List<Attachments> attachmentsList = attachmentsMapper.selectAttachmentByBizId(activityInfo.getId());
                if (attachmentsList != null && !attachmentsList.isEmpty()) {
                    List<AttachmentsDto> attachmentsDtoList = attachmentsList.stream().map(AttachmentsConvertor::convertToDto).collect(Collectors.toList());
                    activityInfoDto.setImageDetails(attachmentsDtoList);
                }
                Date currentTime = new Date();
                if (activityInfoDto.getEndTime() != null) {
                    LocalDate localDate1 = currentTime.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
                    LocalDate localDate2 = activityInfoDto.getEndTime().toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
                    activityInfoDto.setAvailable(!localDate1.isAfter(localDate2));
                }
                dtoList.add(activityInfoDto);
            }
            result.setData(dtoList);
            result.setPageInfo(pageDomain);
        }
        return result;
    }

    /**
     * 根据活动ID获取活动信息。
     *
     * @param id 活动的唯一标识符。
     * @return 返回一个ResponseMessage对象，如果活动存在且未结束，则返回成功的响应消息并附带活动信息；如果活动不存在或已结束，则返回错误的响应消息。
     */
    @Override
    public ResponseMessage getActivityInfoById(String id) {
        ResponseMessage result;
        // 通过ID查询活动信息
        ActivityInfo activityInfo = activityInfoMapper.selectActivityInfoById(id);
        if (activityInfo != null) {
            ActivityInfoDto activityInfoDto = ActivityInfoConvertor.convertToDto(activityInfo);
            // 检查活动是否已经结束
            // 活动存在且未结束，返回成功响应
            List<Attachments> attachmentsList = attachmentsMapper.selectAttachmentByBizId(activityInfo.getId());
            if (attachmentsList != null && !attachmentsList.isEmpty()) {
                List<AttachmentsDto> attachmentsDtoList = attachmentsList.stream().map(AttachmentsConvertor::convertToDto).collect(Collectors.toList());
                activityInfoDto.setImageDetails(attachmentsDtoList);
            }
            Date currentTime = new Date();
            if (activityInfoDto.getEndTime() != null) {
                LocalDate localDate1 = currentTime.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
                LocalDate localDate2 = activityInfoDto.getEndTime().toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
                activityInfoDto.setAvailable(!localDate1.isAfter(localDate2));
            }
            result = ResponseMessageUtility.createSuccess();
            result.setData(activityInfoDto);
        } else {
            // 活动不存在，返回错误响应
            result = ResponseMessageUtility.createError("活动不存在");
        }
        return result;
    }

    @Override
    public ResponseMessage updateActivityInfo(ActivityInfoDto activityInfoDto) {
        List<String> images = activityInfoDto.getImage();
        if (images != null && !images.isEmpty()) {
            attachmentsService.batchUpdateBizIds(images, activityInfoDto.getId());
        }
        activityInfoMapper.updateActivityInfo(ActivityInfoConvertor.convertToEntity(activityInfoDto));
        return ResponseMessageUtility.createSuccess();
    }

    @Override
    public ResponseMessage deleteActivityInfo(String id) {
        activityInfoMapper.deleteActivityInfo(id);
        return ResponseMessageUtility.createSuccess();
    }
}




